﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
using GSLMS.Entity.DTO;

namespace GSLMS.DAL.Oracle
{
   public partial class Module
    {
       private readonly GSLMS.Core.Loger log = new Core.Loger();

       /// <summary>
       ///  增加一条数据
       /// </summary>
       public  bool Add(GSLMS.Entity.Module model)
       {
           bool flag = false;
           OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("P_ID", model.ID??""),
								new OracleParameter("P_MODULENAME", model.ModuleName??""),
								new OracleParameter("P_TITLE", model.Title??""),
								new OracleParameter("P_URL", model.Url??""),
								new OracleParameter("P_MODULEORDER", model.Order),
								new OracleParameter("P_PARENT", model.Parent??""),
								new OracleParameter("P_REMARK", model.Remark??""),
								new OracleParameter("P_GUID", model.GUID??""),
								new OracleParameter("P_ADXML", model.AdXml??""),
								new OracleParameter("P_TYPES", model.Types),
								new OracleParameter("P_PARMFORMAT", model.ParmFormat??""),
								new OracleParameter("P_ONCILCK", model.OnCilck??""),
								new OracleParameter("P_CSSCLASS", model.CssClass??""),
								new OracleParameter("P_TARGET", model.Target??""),
								new OracleParameter("P_EXTERNAL", model.External??""),
								new OracleParameter("P_FRESH", model.Fresh??""),
								new OracleParameter("P_WARN", model.Warn??""),
								new OracleParameter("P_REVERSE", model.Reverse==true?1:0),
								new OracleParameter("P_CALLBACK", model.CallBack??""),
								new OracleParameter("P_MODULEINDEX", model.Index)};

           for (int i = 0; i < 20; i++)
           {
               parms[i].Direction = ParameterDirection.Input;
           }

           try
           {
               OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_module_add", parms);
               flag = true;
               //清除缓存
               OracleHelper.RemoveCacheObject("_ModuleInfoList");
           }
           catch (Exception ex)
           {
               log.ExceptionHand(ex);
           }
           return flag;
       }

       public bool SaveAll(List<GSLMS.Entity.Module> models)
       {
           bool flag = false;
           string sql = "delete from TB_SYS_MODULE";
           if (models != null && models.Count > 0)
           {
               OracleConnection con = new OracleConnection(OracleHelper.connectionString);
               con.Open();
               OracleTransaction tran = con.BeginTransaction();
               OracleCommand cmd = con.CreateCommand();
               cmd.Transaction = tran;

               cmd.Parameters.Clear();
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = sql;
               int num1 = cmd.ExecuteNonQuery();
               if (num1 > 0)
               {

                   foreach (var model in models)
                   {
                       OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("P_ID", model.ID??""),
								new OracleParameter("P_MODULENAME", model.ModuleName??""),
								new OracleParameter("P_TITLE", model.Title??""),
								new OracleParameter("P_URL", model.Url??""),
								new OracleParameter("P_MODULEORDER", model.Order==null?0:model.Order),
								new OracleParameter("P_PARENT", model.Parent??""),
								new OracleParameter("P_REMARK", model.Remark??""),
								new OracleParameter("P_GUID", model.GUID??""),
								new OracleParameter("P_ADXML", model.AdXml??""),
								new OracleParameter("P_TYPES", model.Types==null?0:model.Types),
								new OracleParameter("P_PARMFORMAT", model.ParmFormat??""),
								new OracleParameter("P_ONCILCK", model.OnCilck??""),
								new OracleParameter("P_CSSCLASS", model.CssClass??""),
								new OracleParameter("P_TARGET", model.Target??""),
								new OracleParameter("P_EXTERNAL", model.External??""),
								new OracleParameter("P_FRESH", model.Fresh??""),
								new OracleParameter("P_WARN", model.Warn??""),
								new OracleParameter("P_REVERSE", model.Reverse==null?0:(model.Reverse==true?1:0)),
								new OracleParameter("P_CALLBACK", model.CallBack??""),
								new OracleParameter("P_MODULEINDEX", model.Index==null?0:model.Index)};

                       for (int i = 0; i < 20; i++)
                       {
                           parms[i].Direction = ParameterDirection.Input;
                       }

                       cmd.Parameters.Clear();
                       cmd.Parameters.AddRange(parms);
                       cmd.CommandType = CommandType.StoredProcedure;
                       cmd.CommandText = "pkg_sys.proc_module_add";

                       int num = cmd.ExecuteNonQuery();
                       if (num <= 0)
                       {
                           tran.Rollback();

                           cmd.Dispose();
                           tran.Dispose();
                           con.Close();
                           con.Dispose();
                           return false;
                       }

                   }

                   flag = true;
                   tran.Commit();
               }
               else
               {
                   flag = false;
                   tran.Rollback();
               }


               cmd.Dispose();
               tran.Dispose();
               con.Close();
               con.Dispose();
           }

           return flag;
       }

       /// <summary>
       ///  更新一条数据
       /// </summary>
       public bool Update(GSLMS.Entity.Module model)
       {
           bool flag = false;
           OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_MODULENAME", model.ModuleName),
								new OracleParameter("P_TITLE", model.Title),
								new OracleParameter("P_URL", model.Url),
								new OracleParameter("P_MODULEORDER", model.Order),
								new OracleParameter("P_PARENT", model.Parent),
								new OracleParameter("P_REMARK", model.Remark),
								new OracleParameter("P_GUID", model.GUID),
								new OracleParameter("P_ADXML", model.AdXml),
								new OracleParameter("P_TYPES", model.Types),
								new OracleParameter("P_PARMFORMAT", model.ParmFormat),
								new OracleParameter("P_ONCILCK", model.OnCilck),
								new OracleParameter("P_CSSCLASS", model.CssClass),
								new OracleParameter("P_TARGET", model.Target),
								new OracleParameter("P_EXTERNAL", model.External),
								new OracleParameter("P_FRESH", model.Fresh),
								new OracleParameter("P_WARN", model.Warn),
								new OracleParameter("P_REVERSE", model.Reverse),
								new OracleParameter("P_CALLBACK", model.CallBack),
								new OracleParameter("P_MODULEINDEX", model.Index)};

           for (int i = 0; i < 20; i++)
           {
               parms[i].Direction = ParameterDirection.Input;
           }

           try
           {
               OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_module_update", parms);
               flag = true;

               //清除缓存
               OracleHelper.RemoveCacheObject("_ModuleInfoList");
           }
           catch (Exception ex)
           {
               log.ExceptionHand(ex);
           }
           return flag;
       }

       /// <summary>
       ///  删除一条数据
       /// </summary>
       public  bool Delete(string ID)
       {
           bool flag = false;
           OracleParameter[] parms = new OracleParameter[]{
								new OracleParameter("P_ID", ID)};

           parms[0].Direction = ParameterDirection.Input;

           try
           {
               OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_module_delete", parms);
               flag = true;
               //清除缓存
               OracleHelper.RemoveCacheObject("_ModuleInfoList");
           }
           catch (Exception ex)
           {
               log.ExceptionHand(ex);
           }
           return flag;
       }

       /// <summary>
       ///DataRowToModel 
       /// </summary>
       private GSLMS.Entity.Module DataRowToModel(DataRow dr)
       {
           if (dr != null && dr.ItemArray.Length > 0)
           {
               GSLMS.Entity.Module model = new GSLMS.Entity.Module();
               if (dr["ID"] != null && dr["ID"].ToString() != "")
               {
                   model.ID =dr["ID"].ToString();
               }
               if (dr["MODULENAME"] != null && dr["MODULENAME"].ToString() != "")
               {
                   model.ModuleName = dr["MODULENAME"].ToString();
               }
               if (dr["TITLE"] != null && dr["TITLE"].ToString() != "")
               {
                   model.Title = dr["TITLE"].ToString();
               }
               if (dr["URL"] != null && dr["URL"].ToString() != "")
               {
                   model.Url = dr["URL"].ToString();
               }
               if (dr["MODULEORDER"] != null && dr["MODULEORDER"].ToString() != "")
               {
                   model.Order = OracleHelper.ToInt(dr["MODULEORDER"].ToString());
               }
               if (dr["PARENT"] != null && dr["PARENT"].ToString() != "")
               {
                   model.Parent = dr["PARENT"].ToString();
               }
               if (dr["REMARK"] != null && dr["REMARK"].ToString() != "")
               {
                   model.Remark = dr["REMARK"].ToString();
               }
               if (dr["GUID"] != null && dr["GUID"].ToString() != "")
               {
                   model.GUID =dr["GUID"].ToString();
               }
               if (dr["ADXML"] != null && dr["ADXML"].ToString() != "")
               {
                   model.AdXml = dr["ADXML"].ToString();
               }
               if (dr["TYPES"] != null && dr["TYPES"].ToString() != "")
               {
                   model.Types = OracleHelper.ToInt(dr["TYPES"].ToString());
               }
               if (dr["PARMFORMAT"] != null && dr["PARMFORMAT"].ToString() != "")
               {
                   model.ParmFormat = dr["PARMFORMAT"].ToString();
               }
               if (dr["ONCILCK"] != null && dr["ONCILCK"].ToString() != "")
               {
                   model.OnCilck = dr["ONCILCK"].ToString();
               }
               if (dr["CSSCLASS"] != null && dr["CSSCLASS"].ToString() != "")
               {
                   model.CssClass = dr["CSSCLASS"].ToString();
               }
               if (dr["TARGET"] != null && dr["TARGET"].ToString() != "")
               {
                   model.Target = dr["TARGET"].ToString();
               }
               if (dr["EXTERNAL"] != null && dr["EXTERNAL"].ToString() != "")
               {
                   model.External = dr["EXTERNAL"].ToString();
               }
               if (dr["FRESH"] != null && dr["FRESH"].ToString() != "")
               {
                   model.Fresh = dr["FRESH"].ToString();
               }
               if (dr["WARN"] != null && dr["WARN"].ToString() != "")
               {
                   model.Warn = dr["WARN"].ToString();
               }
               if (dr["REVERSE"] != null && dr["REVERSE"].ToString() != "")
               {
                   model.Reverse =dr["REVERSE"].ToString().Trim()=="1"?true:false;
               }
               if (dr["CALLBACK"] != null && dr["CALLBACK"].ToString() != "")
               {
                   model.CallBack = dr["CALLBACK"].ToString();
               }
               if (dr["MODULEINDEX"] != null && dr["MODULEINDEX"].ToString() != "")
               {
                   model.Index = OracleHelper.ToInt(dr["MODULEINDEX"].ToString());
               }
               return model;
           }
           else
           {
               return null;
           }
       }

       private DataTable GetAllByCache()
       {


           DataTable dt = null;
           if (OracleHelper.GetCachecObject("_ModuleInfoList") != null)
               dt = (DataTable)OracleHelper.GetCachecObject("_ModuleInfoList");
           if (dt == null || dt.Rows.Count == 0)
           {
               string sql = "";
               sql = "select * from TB_SYS_MODULE";
               DataSet ds = OracleHelper.ExecuteDataset(sql);
               if (ds != null && ds.Tables.Count > 0)
               {
                   dt = ds.Tables[0];
                   OracleHelper.AddCacheObject("_ModuleInfoList", (object)dt, 0);
               }

           }

           return dt;
       }


       /// <summary>
       /// 提取所有模块信息
       /// </summary>
       /// <returns></returns>
       public List<GSLMS.Entity.Module> GetAll()
       {
           var list = new List<GSLMS.Entity.Module>();
           DataTable dt = GetAllByCache();
           if (dt != null && dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   GSLMS.Entity.Module model = new GSLMS.Entity.Module();
                   model = DataRowToModel(dt.Rows[i]);
                   list.Add(model);
               }
           }

           return list;
       }

       public List<GSLMS.Entity.Module> GetAll(UniversallySearchParam searchparma)
       {
           var list = new List<GSLMS.Entity.Module>();
           DataTable dt =null;
           string sqlWhere = " 1=1 ";

           dt = OracleHelper.DataTableFileter(GetAllByCache(), sqlWhere, "");


           if (dt != null && dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   GSLMS.Entity.Module model = new GSLMS.Entity.Module();
                   model = DataRowToModel(dt.Rows[i]);
                   list.Add(model);
               }
           }

           return list;
       }

       /// <summary>
       /// 得到一个对象实体
       /// </summary>
       public  GSLMS.Entity.Module GetByID(string ID)
       {

           GSLMS.Entity.Module sModel = new GSLMS.Entity.Module();
           DataTable dt = OracleHelper.DataTableFileter(GetAllByCache(), " id='" + ID.Trim() + "'", "");
           if (dt!=null&&dt.Rows.Count>0)
           {
               sModel = DataRowToModel(dt.Rows[0]);
           }
           else
           {
               return null;
           }
           return sModel;
       }

       /// <summary>
       /// 分页提取数据
       /// </summary>
       public  List<GSLMS.Entity.Module> GetList(GSLMS.Entity.Module searchParam)
       {
           var list = new List<GSLMS.Entity.Module>();
           DataTable dt = null;
           string tblName = "TB_SYS_MODULE";
           string sqlWhere = "";
           string strSort = "";
           int pageSize = 20;
           int pageIndex = 1;
           string strFields = "*";
           int totalCount = 0;
           int countFlag = 1;
           dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex, countFlag, sqlWhere, out totalCount);
           if (dt != null && dt.Rows.Count > 0)
           {
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   GSLMS.Entity.Module model = new GSLMS.Entity.Module();
                   model = DataRowToModel(dt.Rows[i]);
                   list.Add(model);
               }
           }
           
           return list;
       }

    }
}
